from .db_context import DBConnection
from ..model.user import User

async def get_stuID(qqid):
    cur = DBConnection().connection.cursor()
    cur.execute('''
    SELECT studentId
    FROM User
    WHERE qqid=:qqid
    ''', {"qqid": qqid})
    result = cur.fetchone()
    cur.close()
    return result

async def get_stu(qqid):
    cur = DBConnection().connection.cursor()
    cur.execute('''
    SELECT *
    FROM User
    WHERE qqid=:qqid
    ''', {"qqid": qqid})
    result = cur.fetchone()
    cur.close()
    return result


# 将新的学生的信息插入到user的表中；
async def insert_user(user: User):
    conn = DBConnection().connection
    cur = conn.cursor()
    cur.execute("insert into User values (?,?,?)", (user.qqid, user.studentId, user.name))
    conn.commit()


# 删除某个学生的信息（解除绑定）
async def delete_user(qqid):
    conn = DBConnection().connection
    cur = conn.cursor()
    cur.execute("delete from User where qqid=:qqid", {"qqid": qqid})
    conn.commit()


# 查询某个QQ号是否绑定学号,若之前没有绑定则返回-1，反之则返回原先绑定的学号
async def search_and_check(qqid):
    conn = DBConnection().connection
    cur = conn.cursor()
    cur.execute("select studentId from User where qqid=:qqid", {"qqid": qqid})
    list = cur.fetchone()
    if list is None:
        return -1
    conn.commit()
    return list[0]
